package com.qf.order.insert.service.controller;

import com.qf.order.insert.service.beans.Orders;
import com.qf.order.insert.service.beans.Person;
import com.qf.order.insert.service.service.OrderService;
import com.qf.order.insert.service.service.PersonService;
import com.qfedu.util.JsonTools;
import com.qfedu.vo.ResultVo;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.ObjectReader;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 订单服务
 * 作者：罗小宇
 * 日期：2020/7/3
 */
@RestController
@RequestMapping("order")
public class OrderController {
    @Resource
    private OrderService orderService;
    @Resource
    private PersonService personService;
    /**
     * 插入订单服务并添加入住人信息
     * @param orders
     * @param person
     * @return ReultVo
     *      (待做)请求头获取token并解析获得userId
     */
    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    public ResultVo insert(@RequestParam("orders") String orders, @RequestParam("person")String person){

        // 将获取的字符串转成对象
        Orders orders1 = JsonTools.string2Obj(orders, Orders.class);
        Person person1 = JsonTools.string2Obj(person, Person.class);
        System.out.println(orders);

        //插入入住人信息获取person_id并设置到orders
        personService.insert(person1);
        orders1.setPersonId(person1.getPersonId());

        //生成随机时间作为order_id
        String orderId = String.valueOf(System.currentTimeMillis());
        orders1.setOrderId(orderId);

        //测试数据userId（应该由前端通过请请求头传token过来解析）
        Integer userId = 1;
        orders1.setUserId(userId);
        //插入订单表
        orderService.insertOrder(orders1);

        return ResultVo.getSuccessResultVo("success",orderId);
    }
}
